package com.ksyzt.ksite2.test.sms;

import java.io.InputStream;
import java.io.OutputStream;

import javax.comm.CommPortIdentifier;
import javax.comm.SerialPort;
public class SerialToGsm {

    InputStream in;
    OutputStream out;
    String lastIndexRead;
    String[] senderNum = new String[50];
    String[] smsMsg = new String[50];
    int smsCount;

    SerialToGsm(String porta,int BaudRate) {
        try {
            CommPortIdentifier portId = CommPortIdentifier.getPortIdentifier(porta);
            SerialPort sp = (SerialPort)portId.open("Sms_GSM", 0);
            sp.setSerialPortParams(BaudRate, SerialPort.DATABITS_8, SerialPort.STOPBITS_1, SerialPort.PARITY_NONE);
            sp.setFlowControlMode(sp.FLOWCONTROL_NONE);

            in = sp.getInputStream();
            out = sp.getOutputStream();

            // modem init
           // sendAndRecv("AT", 30);       // delay for 20 sec/10
            sendAndRecv("ATE0", 30);        // echo off
           // sendAndRecv("AT +CMEE=1", 30);  // verbose error messages
            sendAndRecv("AT+CMGF=0", 70);   // set pdu mode

        }
        catch (Exception e) {
            System.out.println("Exception " + e);
	          System.exit(1);
        }
    }

    private String sendAndRecv(String s, int timeout) {
        try {
            // clean serial port input buffer
            in.skip(in.available());
           // System.out.println("=> " + s);
            s = s + "\r";         // add CR
            out.write(s.getBytes());
            out.flush();            
            
            String strIn = new String();
            for (int i = 0; i < timeout; i++){
                int numChars = in.available();
                if (numChars > 0) {
                    byte[] bb = new byte[numChars];
                    in.read(bb,0,numChars);
                    strIn += new String(bb);
                }
                // start exit conditions
                // ---------------------
                if (strIn.indexOf(">\r\n") != -1) {
                    break;
                }                                         
                
                if (strIn.indexOf("OK\r\n") != -1){
                    break;
                }                                         
                
                if (strIn.indexOf("ERROR") != -1) { // if find 'error' wait for CR+LF
                    if (strIn.indexOf("\r\n",strIn.indexOf("ERROR") + 1) != -1) {
                        break;                                             
                    }
                }                                         
                
		            Thread.sleep(100); // delay 1/10 sec
            }

        //    System.out.println("<= " + strIn);

            if (strIn.length() == 0) {
                return "ERROR: len 0";
            }

            return strIn.trim();
        }
        catch (Exception e) {                  
            System.out.println("send e recv Exception " + e);
            return "ERROR: send e recv Exception";
        }
    }

    public String sendSms (String numToSend, String whatToSend) {
        ComputeSmsData sms = new ComputeSmsData();
        sms.setAsciiTxt(whatToSend);
        sms.setTelNum(numToSend);

        String s = new String();
        s = sendAndRecv("AT+CMGS=" + (sms.getCompletePduData().length() / 2) + "\r", 30);

        if (s.indexOf(">") != -1) {

            s = sendAndRecv("00" + sms.getCompletePduData() + "\u001A", 150);
            if (s.indexOf("OK") != -1){ s = "OK";}
            return s;
        }              
        else {
            return "ERROR";
        }
    }
                         
    // used to reset message data
    private void resetGsmObj() {
        lastIndexRead = null;
        senderNum = null;
        smsMsg = null;
    }


    public int checkSms (){
        String str = new String();
        String strGsm = new String();                          
        strGsm = sendAndRecv("AT+CMGL=0", 1000);  // list unread msg and sign them as read

        // if answer contain ERROR then ERROR
        if (strGsm.indexOf("ERROR") != -1) {
            resetGsmObj();
            return -1; // error
        }
        
        strGsm = sendAndRecv("AT+CMGL=1", 1000);  // list read msg
        // if answer contain ERROR then ERROR
        if (strGsm.indexOf("ERROR") != -1) {
            resetGsmObj();
            return -2; // error
        }
        
        if (strGsm.equals("OK")) {
            resetGsmObj();
            return 0; // only ok,no sms
        }

        // evaluate message index
        if (strGsm.indexOf(':') <= 0) {
            resetGsmObj();
            //return ("ERROR unexpected answer");
            return -3;
        }

       // System.out.println("strGsm= " + strGsm);
        smsCount = 0;
        Initsmsbuffer();
        ComputeSmsData sms = new ComputeSmsData();
        do{
          str = strGsm.substring(strGsm.indexOf(':') + 1,strGsm.indexOf(','));
          str = str.trim(); // remove white spaces

          lastIndexRead = str;
        
                                                 
          // find message string
          // -------------------
          // look for start point (search \r, then skip \n, add and one more for right char
          int startPoint = strGsm.indexOf("\r",(strGsm.indexOf(":") + 1)) + 2;
          int endPoint = strGsm.indexOf("\r",startPoint + 1);
          if (endPoint == -1) {
              // only one message or the last message
              endPoint = strGsm.length();
          }

          // extract string
          str = strGsm.substring(startPoint, endPoint);
         // System.out.println("str= " + str);

      //    ComputeSmsData sms = new ComputeSmsData();
          sms.setRcvdPdu(str);
  //        SMSCNum = new String(sms.getRcvdPduSMSC());
          senderNum[smsCount] = new String(sms.getRcvdSenderNumber());
          smsMsg[smsCount] = new String(sms.getRcvdPduTxt());
          smsCount = smsCount + 1;
          if(endPoint >= strGsm.length()) break;
          strGsm = strGsm.substring(endPoint+1, strGsm.length());
        }
        while(!strGsm.trim().equals("OK"));

        
        delAllSms();
        return smsCount;
    }

    public String readSmsSender(int i) {
        return senderNum[i];
    }
    
    public String readSms(int i) {
        return smsMsg[i];
    }
    
    public void Initsmsbuffer(){
    	  int i;
    	  for(i=0;i<50;i++){
    	  	senderNum[i] = "";
    	  	smsMsg[i] = "";
    	  }
    }
    
    public String delSms() {   
        if (lastIndexRead != "") {                
            return sendAndRecv("AT+CMGD=" + lastIndexRead, 30);
        }
        return ("ERROR");
    }
    
    public String delAllSms() {   
        return sendAndRecv("AT+CMGD=1,4", 300);
    }
}
